package com.irdstudio.biz.drools.api;

import com.irdstudio.sdk.beans.web.controller.AbstractController;
import com.irdstudio.biz.drools.beans.RuleBean;
import com.irdstudio.biz.drools.exception.FireRuleErrorEnum;
import com.irdstudio.biz.drools.exception.FireRuleException;
import com.irdstudio.biz.drools.service.FireRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/api")
public class FireRuleController extends AbstractController {


    @Autowired
    @Qualifier("fireRuleServiceImpl")
    private FireRuleService fireRuleService;


    /**
     * 规则集执行接口
     * @param request
     * @return
     */
    @RequestMapping(value="/esb/fire", method= RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fireRules(@RequestBody RuleBean request) {
        Map<String, Object> resp = null;
        try {
            resp = fireRuleService.fire(request);
            resp.put("errorCode", FireRuleErrorEnum.SUCCESS.getCode());
            resp.put("errorMsg", FireRuleErrorEnum.SUCCESS.getMessage());
        } catch (Exception e) {
            resp = handleException(e);
        }
        return resp;
    }


    /**
     * 规则集列表查询 带分页
     * @param request
     * @return
     */
    @RequestMapping(value="/esb/list", method=RequestMethod.POST)
    public @ResponseBody Map<String, Object> list(@RequestBody Map<String, Object> request) {
        Map<String, Object> resp = null;
        try {
            resp = fireRuleService.list(request);
        } catch (Exception e) {
            resp = handleException(e);
        }
        return resp;
    }

    /**
     * 处理异常信息，输出
     * @param e
     * @return
     */
    private Map<String, Object> handleException(Exception e) {
        logger.error(e.getMessage(), e);
        Map<String, Object> errorMap = new HashMap<>(2);
        if (e instanceof FireRuleException) {
            errorMap.put("errorCode", ((FireRuleException)e).getErrorCode());
            errorMap.put("errorMsg", ((FireRuleException)e).getErrorMsg());
        } else if (e.getCause() instanceof FireRuleException) {
            errorMap.put("errorCode", ((FireRuleException)e.getCause()).getErrorCode());
            errorMap.put("errorMsg", ((FireRuleException)e.getCause()).getErrorMsg());
        } else {
            errorMap.put("errorCode", FireRuleErrorEnum.E_OTHER.getCode());
            errorMap.put("errorMsg", FireRuleErrorEnum.E_OTHER.getMessage(e.getMessage()));
        }

        return errorMap;
    }
}
